package com.ete.lib.upload.ui

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import androidx.appcompat.widget.AppCompatTextView
import com.blankj.utilcode.util.ColorUtils
import com.ete.lib.upload.R
import com.ete.lib.upload.logZip.UploadLogUtil

/**
 * Author:LiXian
 * <p>
 * Desc:TODO
 * Version:1.0
 * date 2024/1/9 15:28
 */
class CornerButton (context: Context, attr: AttributeSet?, defStyleAttr: Int) :
        AppCompatTextView(context, attr, defStyleAttr) {

    private var mPaint: Paint
    private var mBgCorner: Float
    private val mBgRectF: RectF = RectF()

    companion object {
        private const val DEFAULT_BG_CORNER = 9F //dp
    }

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attr: AttributeSet?) : this(context, attr, 0)

    init {
        gravity = Gravity.CENTER
        mPaint = Paint().apply {
            style = Paint.Style.FILL
            val themeColor = if (UploadLogUtil.mUploadEntry.mAppThemeColor == 0) {
                R.color.color_E7EAF2
            } else {
                UploadLogUtil.mUploadEntry.mAppThemeColor
            }
            color = ColorUtils.getColor(themeColor)
        }
        mBgCorner = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                DEFAULT_BG_CORNER, context.resources.displayMetrics)
    }

    override fun onDraw(canvas: Canvas?) {
        mBgRectF.set(0F, 0F, measuredWidth.toFloat(), measuredHeight.toFloat())
        canvas?.let {
            canvas.drawRoundRect(mBgRectF, mBgCorner, mBgCorner, mPaint)
        }
        super.onDraw(canvas)
    }
}